package com.delivery.www.servlet;

import com.delivery.www.po.Seller;
import com.delivery.www.po.User;
import com.delivery.www.service.ServiceImpl.SellerServiceImpl;
import com.delivery.www.service.ServiceImpl.UserServiceImpl;

import java.util.List;
import java.util.Scanner;

public class RegisterPage {
    public static boolean registerUser() {
        UserServiceImpl userService = new UserServiceImpl();
        SellerServiceImpl sellerService = new SellerServiceImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择注册账号类型：");
        System.out.println("1.用户");
        System.out.println("2.商家");
        int i = scanner.nextInt();
        while (i != 1 && i != 2) {
            System.out.println("输入错误！请重新输入");
            i = scanner.nextInt();
        }
        boolean b = false;
        String name = "";
        if (i == 1) {
            boolean flag;
            do {
                flag = false;
                System.out.println("请输入用户名：");
                name = scanner.next();
                List<User> users = userService.getUser();
                for (User user : users) {
                    if (user.getUserName().equals(name)) {
                        System.out.println("用户名已存在请重新输入");
                        flag = true;
                        break;
                    }
                }
            } while (flag);
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请输入邮箱：");
            String email = scanner.next();
            b = userService.register(name, password, email);
        }
        if (i == 2) {
            boolean flag;
            do {
                flag = false;
                System.out.println("请输入用户名：");
                name = scanner.next();
                List<Seller> sellers = sellerService.getSeller();
                for (Seller seller : sellers) {
                    if (seller.getSellerName().equals(name)) {
                        System.out.println("用户名已存在请重新输入");
                        flag = true;
                        break;
                    }
                }
            } while (flag);
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请输入邮箱：");
            String email = scanner.next();
            b = sellerService.register(name, password, email);
        }
        return b;
    }
}
